home *** CD-ROM | disk | FTP | other *** search
- Path: news.cencom.net!ns!tanp
- From: tanp@ns (Bill Wendling)
- Newsgroups: comp.lang.c
- Subject: Re: Please help ?!
- Date: 19 Jan 1996 05:21:56 GMT
- Organization: Cen-Com Internet
- Message-ID: <4dn9pk$43o@news.cencom.net>
- References: <4dm889$3hs@neptunus.pi.net>
- NNTP-Posting-Host: ns.cencom.net
- X-Newsreader: TIN [version 1.2 PL2]
-
- mv@pi.net inexplicably wrote:
- } Hello everybody,
-
-
- } Please take a look at this:
-
- } ----------------------------
- } #include <stdio.h>
- } #include <stdlib.h>
- } #include <conio.h>
- } #include <string.h>
- } #include <alloc.h>
-
-
- } #define MAXLEN 20
- } #define MAXSWITCH 20
-
-
- } char *s="dir /w:test/1/2/3/4";
- } char *s2, *s3;
- } char myswitches[MAXSWITCH][MAXLEN];
-
-
- } int get_switches(char *cmd, char *switches[][MAXLEN])
-
- /*
- ACK!!! You didn't pass what you thought you passed.
- char *switches[][MAXLEN]
- is a pointer to a double array...a 3 dimensional array!
- You should define it as:
- char *switches[MAXLEN]
- This will help greatly.
- */
-
-
- } {
- } int i=0;
- } char *cpy;
- } char *saved;
-
- } if (strlen(cmd)==0)
- } return 0;
- }
- } cpy=(char *)malloc(MAXLEN);
-
- } if (!cpy)
- } {
- } printf("Out of memory in %s on line: %d\n",__FILE__,__LINE__);
- } exit(EXIT_FAILURE);
- } }
-
- } strcpy(cpy,'\0');
-
- /*
- This should be:
- strcpy(cpy, "\0");
- since it expects a char * in the second parm
-
- Better yet, you could just say:
- cpy[0] = '\0';
- or
- *cpy = '\0';
- */
-
- } saved=cpy;
-
- } while (*cmd!='\0')
- } {
- } if (*cmd=='/')
- } {
- } if (i==MAXSWITCH)
-
- /*
- Better is:
- if (i >= MAXSWITCH)
- since it will assure you that if i goes over MAXSWITCH, it will do this.
- */
-
- } {
- } printf("INT: Too many switches ...\n");
- } return 9999;
- } }
-
- } *cpy=*cmd;
- } do { *cpy++=*cmd++; } while (!strchr(" /-\0",*cmd));
-
- } *cpy='\0';
- } cpy=saved;
- } strcpy(switches[i],cpy);
- } strcpy(cpy,'\0');
-
- /*
- strcpy(cpy, "\0");
- */
-
- } i++;
- } }
-
- /*
- I:
-
- Put and "else" here (see II at bottom).
- */
-
- } if (*cmd=='-')
- } {
- } if (i==MAXSWITCH)
-
- /*
- Again:
- if (i >= MAXSWITCH)
- */
-
- } {
- } printf("INT: Too many switches ...\n");
- } return 9999;
- } }
-
- } *cpy=*cmd;
- } do { *cpy++=*cmd++; } while (!strchr(" /-\0",*cmd));
- } *cpy='\0';
- } cpy=saved;
- } strcpy(switches[i],cpy);
- } strcpy(cpy,'\0');
-
- /*
- strcpy(cpy, "\0");
- */
-
- } i++;
- } }
-
- /*
- II:
-
- Put the code here:
- else
- cmd++;
-
- here.
- */
-
-
- } if (!strchr("/-\0",*cmd))
- *cmd++;
-
- /*
- *cmd++;
- is incrementing what cmd points to at that time...Not good...See above
- else's for a good way to increment through the string.
- */
- } }
-
- } free(cpy);
- } return i;
- } }
-
-
- } int main (void)
-
-
- } {
- } int i;
-
- } clrscr();
- } i=get_switches(s,myswitches);
- } printf("Switches found: %i\n",i);
- } for (i=0; i<MAXSWITCH; i++)
- } {
-
-
- } /*
-
- } Here it should print:
-
- } Switch: /w:test
- } Switch: /1
- } etc..
-
- } but it prints:
-
- } Switch: /w:test
- } Switch:
- } Switch: /1
- } and so on....
- } It jjust skips one array index everytime... It seems to go wrong in get_switches()...
- } */
- } printf("Switch: %s\n",myswitches[i]);
- } }
- } return 0;
- } }
-
- Other than those things I mentioned, it looks alright. The
- char *switches[][MAXLEN] problem may be the real culprit.
-
- } TTYL,
-
- } Martijn....
-
-
- --
- Bill Wendling | "Pinky, are you thinking what I'm thinking?"
- tanp@ns.cencom.net | "I think so, Brain, but burlap chafes me so."
- "Boom Shanka" | Finger me for my Geek Code...NOW!
-